package com.jia.together.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.together.entity.User;
import com.jia.together.entity.Permission;
import com.jia.together.entity.User;
import com.jia.together.exception.MyException;
import com.jia.together.mapper.UserMapper;
import com.jia.together.service.PermissionService;
import com.jia.together.service.RolePermissionService;
import com.jia.together.service.UserRoleService;
import com.jia.together.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.together.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dujiajia
 * @since 2021-02-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public User selectByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = baseMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public List<Permission> selectPermissionByUsername(String username) {
        User user = selectByUsername(username);
        // 获取角色id
        String roleId = userRoleService.selectRoleIdByUserId(user.getId());
        // 根据角色id获取权限id列表
        List<String> permissionIds =  rolePermissionService.selectPermissionIdsByRoleId(roleId);
        // 根据权限id列表获取权限
        List<Permission> permissionList = permissionService.listByIds(permissionIds);
        return permissionList;
    }

    @Override
    public User selectInfoByToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        Claims jwtClaims = JwtUtil.getJwtClaims(token);
        String username = (String) jwtClaims.get("username");
        User user = selectByUsername(username);
        return user;
    }

    @Override
    public String easyLogin(User user) {
        String username = user.getUsername();
        String password = user.getPassword();

        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            throw new MyException(400,"没有用户名或密码");
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User selectOne = baseMapper.selectOne(wrapper);
        if(selectOne == null){
            throw new MyException(400,"用户名或密码错误");
        }
        boolean matches = passwordEncoder.matches(password, selectOne.getPassword());
        if (!matches){
            throw new MyException(400,"用户名或密码错误");
        }

        String jwtTokenByUser = JwtUtil.createJwtTokenByUser(selectOne);
        return jwtTokenByUser;
    }

    @Override
    public Map<String, Object> selectPageList(Long pageNum, Long pageSize) {

        Page<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        Page<User> resourcePage = baseMapper.selectPage(page, wrapper);
        List<User> records = resourcePage.getRecords();
        long total = resourcePage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        return map;

    }
}
